<?php

namespace Registro\Common\Controllers;

use Registro\Models\Users,
    Registro\Frontend\Forms\LoginForm,
    Registro\Frontend\Forms\RegisterForm,
    Registro\Auth\Exception as AuthException;

/**
 * SessionController
 *
 * Allows to authenticate users
 */
class AuthController extends ControllerBase {

    public function initialize()
    {
        $this->tag->appendTitle( 'Sign Up/Sign In' );
        parent::initialize();
    }

    public function indexAction()
    {
              echo "<pre>";
        var_dump($this->auth->getIdentity());
        echo "</pre>";
        if( !$this->request->isPost() )
        {
            $this->tag->setDefault( 'user_username', 'admin@test.it' );
            $this->tag->setDefault( 'user_password', 'testtest' );
        }
    }

    /**
     * Register an authenticated user into session data
     *
     * @param Users $user
     */
    private function _registerSession( Users $user )
    {
        $this->session->set( 'auth', array(
            'user_id' => $user->user_id,
            'user_displayname' => $user->user_displayname
        ) );
    }

    /**
     * This action authenticate and logs an user into the application
     *
     */
    public function loginAction()
    {
        $form = new LoginForm();

        try{

            if( !$this->request->isPost() )
            {
                $this->tag->setDefault( 'user_username', 'admin@test.it' );
                $this->tag->setDefault( 'user_password', 'testtest' );
                
                if( $this->auth->hasRememberMe() )
                {
                    return $this->auth->loginWithRememberMe();
                }
            }
            else
            {
                if( $form->isValid( $this->request->getPost() ) == false )
                {
                    foreach( $form->getMessages() as $message ){
                        $this->flash->error( $message );
                    }
                }
                else
                { 
                    $this->auth->check( array(
                        'user_username' => $this->request->getPost( 'user_username' ),
                        'user_password' => $this->request->getPost( 'user_password' ),
                        'remember' => $this->request->getPost( 'remember' )
                    ) );

                    return $this->response->redirect( 'auth' );
                }
            }
        } catch(AuthException $e){
            $this->flash->error( $e->getMessage() );
        }

        $this->view->loginForm = $form;
//        $form = new LoginForm;
//        
//        if( $this->request->isPost() )
//        {
//
//            $user_username = $this->request->getPost( 'user_username' );
//            $user_password = $this->request->getPost( 'user_password' );
//
//            $user = Users::findFirst( array(
//                        "(user_email = :user_username: OR user_displayname = :user_username:) AND user_password = :user_password: AND user_active = '1'",
//                        'bind' => array( 'user_username' => $user_username, 'user_password' => md5( $user_password ) )
//            ) );
//            
//            if( $user != false )
//            {
//                $this->_registerSession( $user );
//                $this->flash->success( 'Welcome ' . $user->user_displayname );
//                return $this->forward( 'index/index' );
//            }
//            
//            $this->flash->error( 'Wrong email/password' );
//        }
//
//        $this->view->loginForm = $form;
    }

    /**
     * Finishes the active session redirecting to the index
     *
     * @return unknown
     */
    public function logoutAction()
    {
        $this->auth->remove();
        //$this->session->remove( 'auth-identity' );
        $this->flash->success( 'Goodbye!' );
        return $this->dispatcher->forward( 'index/index' );
    }

}
